const db = require('../db/index')


// 获取文章详情
exports.getArticleDetail = (req, res) => {
    const sql = `select * from publish where id = ? and type = 1`
    db.query(sql, [req.query.articleId],function (err, results) {
        // 执行 SQL 语句失败
        if (err) return res.cc(err)
        if(results.length!==1){
           return res.cc('获取失败！')
        }
        return res.send({
            status: 0,
            message: '获取成功！',
            data:results[0]
        })
    })

}

// 获取文章评论
exports.getArticleComment = (req, res) => {
    //获取所有文章对应的评论
    const sql = `select * from comments where articleId = ?`
    db.query(sql, [req.query.articleId],function (err, results) {
        // 执行 SQL 语句失败
        if (err) return res.cc(err)
        //对没有父亲id的评论进行整理
        let returnData=[]
        let temp=[]
        for(let i=0;i<results.length;i++){
            if(results[i].parentId===-1){
                temp=results[i]
                temp.children=[]
                for(let j=0;j<results.length;j++){
                    if(results[i].id===results[j].parentId){
                        temp.children.push(results[j])
                    }
                }
                returnData.push(temp)
            }
        }
        return res.send({
            status: 0,
            message: '获取成功！',
            data:returnData
        })
    })

}

// 添加文章评论
exports.addArticleComment = (req, res) => {
    //获取所有文章对应的评论
    const sql = `insert into comments (userId,userName,userAvatar,parentId,parentName,parentAvatar,articleId,content,time) values (?,?,?,?,?,?,?,?,?)`
    // console.log(req.body)
    const body=req.body
    db.query(sql, [body.userId,body.userName,body.userAvatar,body.parentId,body.parentName,body.parentAvatar,body.articleId,body.content,body.time],function (err, results) {
        // 执行 SQL 语句失败
        if (err) return res.cc(err)
        // console.log(res,results)
        return res.send({
            status: 0,
            message: '添加成功！'
        })
    })

}

// 点赞或者不点赞文章
exports.likeArticle = (req, res) => {
    const body=req.body
    // console.log(body)
    //点赞和不点赞，有且只有一个为真
    if(body.isAgree){
        const sql = `insert into likes (publishId,userId,type,isLike) values (?,?,?,?)`
        // console.log(req.body)
        db.query(sql, [body.publishId,req.user.id,body.type,body.isAgree],function (err, results) {
            // 执行 SQL 语句失败
            if (err) return res.cc(err)
            //如果之前有不点赞的记录，需要删除
            const sql3=`select * from likes where publishId=? and userId=? and type =? and isLike=0 and isDelete =0`
            db.query(sql3,[body.publishId,req.user.id,body.type],(err, results3)=>{
                if (err) return res.cc(err)
                if(results3.length>0){
                    // console.log('进来了',results3)
                    const sql4=`update likes set isDelete=1 where publishId=? and userId=? and type =? and isLike=0`
                    db.query(sql4,[body.publishId,req.user.id,body.type],(err, results3)=>{
                        if (err) return res.cc(err)
                    })
                    //同时也得更新publish表中notLikeNum
                    const sql5=`update publish set notLikeNum=notLikeNum-1 where id=?`
                    db.query(sql5,[body.publishId],(err,result)=>{
                        if (err) return res.cc(err)
                    })
                }
            } )

            //更新publish表中的likeNum
            //`update publish set notLikeNum=notLikeNum+1 where id=?`
            const sql2=`update publish set likeNum=likeNum+1 where id=?`
            db.query(sql2,[body.publishId],(err,result)=>{

                if (err) return res.cc(err)
                return res.send({
                    status: 0,
                    message: '成功！'
                })
            })
        
        })
    }
    if(body.isDisagree){
        const sql = `insert into likes (publishId,userId,type,isLike) values (?,?,?,?)`
        // console.log(req.body)
        let temp=!body.isDisagree
        db.query(sql, [body.publishId,req.user.id,body.type,temp],function (err, results) {
            // 执行 SQL 语句失败
            if (err) return res.cc(err)
            //如果之前有点赞的记录，需要删除
            const sql3=`select * from likes where publishId=? and userId=? and type =? and isLike=1 and isDelete =0`
            db.query(sql3,[body.publishId,req.user.id,body.type],(err, results3)=>{
                if (err) return res.cc(err)
                if(results3.length>0){
                    const sql4=`update likes set isDelete=1 where publishId=? and userId=? and type =? and isLike=1`
                    db.query(sql4,[body.publishId,req.user.id,body.type],(err, results3)=>{
                        if (err) return res.cc(err)
                    })
                    //同时也得更新publish表中likeNum
                    const sql5=`update publish set likeNum=likeNum-1 where id=?`
                    db.query(sql5,[body.publishId],(err,result)=>{
                        if (err) return res.cc(err)
                    })
                }
            } )

            //更新publish表中的notLikeNum
            //`update publish set notLikeNum=notLikeNum+1 where id=?`
            const sql2=`update publish set notLikeNum=notLikeNum+1 where id=?`
            db.query(sql2,[body.publishId],(err,result)=>{

                if (err) return res.cc(err)
                return res.send({
                    status: 0,
                    message: '成功！'
                })
            })
        
        })
    }
    //还有可能都为假
    if(!body.isDisagree && !body.isAgree){
        //如果之前有不点赞的记录，需要删除
        const sql3=`select * from likes where publishId=? and userId=? and type =? and isLike=0 and isDelete =0`
        db.query(sql3,[body.publishId,req.user.id,body.type],(err, results3)=>{
            if (err) return res.cc(err)
            if(results3.length>0){
                const sql4=`update likes set isDelete=1 where publishId=? and userId=? and type =? and isLike=0`
                db.query(sql4,[body.publishId,req.user.id,body.type],(err, results3)=>{
                    if (err) return res.cc(err)
                })
                //同时也得更新publish表中notLikeNum
                const sql5=`update publish set notLikeNum=notLikeNum-1 where id=?`
                db.query(sql5,[body.publishId],(err,result)=>{
                    if (err) return res.cc(err)
                })
            }
        } )

        //如果之前有点赞的记录，需要删除
        const sql33=`select * from likes where publishId=? and userId=? and type =? and isLike=1 and isDelete =0`
        db.query(sql33,[body.publishId,req.user.id,body.type],(err, results3)=>{
            if (err) return res.cc(err)
            console.log('gasn',results3)
            if(results3.length>0){
                const sql4=`update likes set isDelete=1 where publishId=? and userId=? and type =? and isLike=1`
                db.query(sql4,[body.publishId,req.user.id,body.type],(err, results3)=>{
                    if (err) return res.cc(err)
                })
                //同时也得更新publish表中likeNum
                const sql5=`update publish set likeNum=likeNum-1 where id=?`
                db.query(sql5,[body.publishId],(err,result)=>{
                    if (err) return res.cc(err)
                })
            }
        } )
    }
}

// 判断是否点赞或者不点赞文章
exports.isLikeArticle = (req, res) => {
    const sql=`select * from likes where publishId=? and userId=? and isLike =1 and isDelete =0`
    db.query(sql,[req.query.publishId,req.user.id],(err,results1)=>{
        // 执行 SQL 语句失败
        if (err) return res.cc(err)
        const sql1=`select * from likes where publishId=? and userId=? and isLike =0 and isDelete =0`
        db.query(sql1,[req.query.publishId,req.user.id],(err,results2)=>{
            // console.log(req.query,req.user.id)
            if (err) return res.cc(err)
            let isLike=false
            let isNotLike=false
            if(results1.length>0){
                isLike=true
            }
            if(results2.length>0){
                isNotLike=true
            }
            return res.send({
                status: 0,
                message: '成功！',
                data:{
                    isLike,
                    isNotLike
                }
            })
        })
    })

}

// 收藏文章
exports.collectArticle = (req, res) => {
    // console.log(req.body)
    let body=req.body
    if(!body.isCollect){
        const sql3 =`update collect set isDelete=1 where articleId=? and userId=?`
        db.query(sql3,[body.articleId,req.user.id],(err,results3)=>{
            if (err) return res.cc(err)
            return res.send({
                status: 0,
                message: '取消成功！'
            })
        })
    }
    else{
        const sql=`select * from collect where articleId=? and userId=? and isDelete =0`
        db.query(sql,[body.articleId,req.user.id],(err,results1)=>{
            if(results1.length>0){
                return res.cc('请勿重复收藏！')
            }
            // console.log(results1)
            const sql1 = `insert into collect (articleId,userId,fileFolderId) values (?,?,?)`
            db.query(sql1,[body.articleId,req.user.id,body.fileFolderId],function (err, results) {
                // 执行 SQL 语句失败
                if (err) return res.cc(err)
                return res.send({
                    status: 0,
                    message: '收藏成功！'
                })
            })
        })
    }
    

}

//判断是否文章被收藏
exports.isCollectArticle = (req, res) => {
    const sql=`select * from collect where articleId=? and userId=? and isDelete = 0`
    db.query(sql,[req.query.articleId,req.user.id],(err,results1)=>{
        // 执行 SQL 语句失败
        if (err) return res.cc(err)
        // console.log(results1,req.body.articleId,req.user.id)
        if(results1.length>0){
            return res.send({
                status: 0,
                message: '成功！',
                data:{
                    isCollect:true
                }
            })
        }else{
            return res.send({
                status: 0,
                message: '成功！',
                data:{
                    isCollect:false
                }
            })
        }
    })
}
